package com.atguigu.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 通过key获取缓存中的val
     * @param key 键
     * @return 值
     */
    public Object getValByKey(String key){
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 添加到缓存
     * @param key 键
     * @param val 值
     * @param expireTime 过期时间
     */
    public void setKeyVal(String key, String val, Long expireTime){
        redisTemplate.opsForValue().set(key, val, expireTime, TimeUnit.SECONDS);
    }
}
